﻿using System.Windows;
using VNet.VisiSuite.Common.Logger;
using VNet.VisiSuite.Controls;
using VNet.VisiSuite.VisiAdmin.Common.Helpers;

namespace VNet.VisiSuite.VisiAdmin.Views
{
    public partial class MainWindow : Window
    {
#if !DEBUG
        private AutoUpdate _auto_update = null;
#endif

        public MainWindow()
        {
            InitializeComponent();

            Title = App.ApplicationNameAndVersion();
            VNetUserControl.SetFrameworkElement(this); // so resources will load properly
            NavigationManager.SetMainWindow(this);
#if !DEBUG
            WindowState = System.Windows.WindowState.Maximized;
            Topmost = true;
            WindowStyle = System.Windows.WindowStyle.None;
            ResizeMode = System.Windows.ResizeMode.NoResize;

            if (_auto_update == null)
            {
                _auto_update = new AutoUpdate();
                _auto_update.Start(360, InstallUpdateCompleted);
            }
#endif
        }

        // This is used in release mode to restart the application after installing an update.
        private bool InstallUpdateCompleted(string errorMessage)
        {
            FileLog.Error("InstallUpdate() - " + errorMessage);
            if (string.IsNullOrEmpty(errorMessage))
            {
                FileLog.Info("The application has been upgraded, and will now restart.");
                System.Windows.Forms.Application.Restart();
                Application.Current.Shutdown();
            }
            else
            {
                FileLog.Error("Error during auto update: " + errorMessage);
            }

            return true;
        }
    }
}